﻿Imports System
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Workflow.Activities.Rules
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.Interop

Namespace System.Workflow.Activities.Rules.Design
    Friend NotInheritable Class RuleSetBrowserDialog
        Inherits BasicBrowserDialog
        ' Methods
        Public Sub New(ByVal activity As Activity, ByVal name As String)
            MyBase.New(activity, name)
            Dim definitions As RuleDefinitions = ConditionHelper.Load_Rules_DT(activity.Site, Helpers.GetRootActivity(activity))
            If (Not definitions Is Nothing) Then
                Me.ruleSetCollection = definitions.RuleSets
            End If
            MyBase.InitializeListView(Me.ruleSetCollection, name)
        End Sub

        Protected Overrides Function GetObjectName(ByVal ruleObject As Object) As String
            Dim set As RuleSet = [TryCast](ruleObject,RuleSet)
            Return [set].Name
        End Function

        Friend Overrides Function IsUniqueName(ByVal ruleName As String) As Boolean
            Return Not Me.ruleSetCollection.Contains(ruleName)
        End Function

        Protected Overrides Sub OnDeleteInternal(ByVal ruleObject As Object)
            Dim set As RuleSet = [TryCast](ruleObject,RuleSet)
            Me.ruleSetCollection.Remove([set].Name)
        End Sub

        Protected Overrides Function OnEditInternal(ByVal currentRuleObject As Object, <Out> ByRef updatedRuleObject As Object) As Boolean
            Dim ruleSet As RuleSet = [TryCast](currentRuleObject,RuleSet)
            updatedRuleObject = Nothing
            [Using] dialog As RuleSetDialog = New RuleSetDialog(MyBase.Activity, ruleSet)
                If (DialogResult.OK = dialog.ShowDialog) Then
                    Me.ruleSetCollection.Remove(ruleSet.Name)
                    Me.ruleSetCollection.Add(dialog.RuleSet)
                    updatedRuleObject = dialog.RuleSet
                    Return True
                End If
            End [Using]
            Return False
        End Function

        Protected Overrides Function OnNewInternal() As Object
            [Using] dialog As RuleSetDialog = New RuleSetDialog(MyBase.Activity, Nothing)
                If (DialogResult.OK = dialog.ShowDialog(Me)) Then
                    Dim ruleSet As RuleSet = dialog.RuleSet
                    ruleSet.Name = Me.ruleSetCollection.GenerateRuleSetName
                    Me.ruleSetCollection.Add(ruleSet)
                    Return ruleSet
                End If
            End [Using]
            Return Nothing
        End Function

        Protected Overrides Function OnRenameInternal(ByVal ruleObject As Object) As String
            Dim item As RuleSet = [TryCast](ruleObject,RuleSet)
            [Using] dialog As RenameRuleObjectDialog = New RenameRuleObjectDialog(MyBase.Activity.Site, item.Name, New NameValidatorDelegate(AddressOf Me.IsUniqueName), Me)
                If ((dialog.ShowDialog(Me) = DialogResult.OK) AndAlso (dialog.RuleObjectName <> item.Name)) Then
                    Me.ruleSetCollection.Remove(item)
                    item.Name = dialog.RuleObjectName
                    Me.ruleSetCollection.Add(item)
                    Return dialog.RuleObjectName
                End If
            End [Using]
            Return Nothing
        End Function

        Protected Overrides Sub UpdateListViewItem(ByVal ruleObject As Object, ByVal listViewItem As ListViewItem)
            Dim set As RuleSet = [TryCast](ruleObject,RuleSet)
            Dim manager As New ValidationManager(MyBase.Activity.Site)
            Dim service As ITypeProvider = DirectCast(manager.GetService(GetType(ITypeProvider)), ITypeProvider)
            Dim validation As New RuleValidation(MyBase.Activity, service, False)
            Dim flag As Boolean = [set].Validate(validation)
            listViewItem.Tag = [set]
            listViewItem.Text = [set].Name
            Dim text As String = If(flag, Messages.Yes, Messages.No)
            If (listViewItem.SubItems.Count = 1) Then
                listViewItem.SubItems.Add([text])
            Else
                listViewItem.SubItems.Item(1).Text = [text]
            End If
        End Sub

        Protected Overrides Sub UpdatePreview(ByVal previewBox As TextBox, ByVal ruleObject As Object)
            Dim ruleSet As RuleSet = [TryCast](ruleObject,RuleSet)
            NativeMethods.SendMessage(previewBox.Handle, 11, IntPtr.Zero, IntPtr.Zero)
            previewBox.Lines = DesignerHelpers.GetRuleSetPreview(ruleSet).Split(New Char() { ChrW(10) })
            NativeMethods.SendMessage(previewBox.Handle, 11, New IntPtr(1), IntPtr.Zero)
            previewBox.Invalidate
        End Sub


        ' Properties
        Protected Overrides ReadOnly Property ConfirmDeleteMessageText As String
            Get
                Return Messages.RuleSetConfirmDeleteMessageText
            End Get
        End Property

        Protected Overrides ReadOnly Property ConfirmDeleteTitleText As String
            Get
                Return Messages.DeleteRuleSet
            End Get
        End Property

        Protected Overrides ReadOnly Property DescriptionText As String
            Get
                Return Messages.RuleSetDescriptionText
            End Get
        End Property

        Friend Overrides ReadOnly Property DuplicateNameErrorText As String
            Get
                Return Messages.RuleSetDuplicateNameErrorText
            End Get
        End Property

        Friend Overrides ReadOnly Property EmptyNameErrorText As String
            Get
                Return Messages.RuleSetEmptyNameErrorText
            End Get
        End Property

        Friend Overrides ReadOnly Property NewNameLabelText As String
            Get
                Return Messages.RuleSetNewNameLableText
            End Get
        End Property

        Protected Overrides ReadOnly Property PreviewLabelText As String
            Get
                Return Messages.RuleSetPreviewLabelText
            End Get
        End Property

        Friend Overrides ReadOnly Property RenameTitleText As String
            Get
                Return Messages.RuleSetRenameTitleText
            End Get
        End Property

        Protected Overrides ReadOnly Property TitleText As String
            Get
                Return Messages.RuleSetTitleText
            End Get
        End Property


        ' Fields
        Private ruleSetCollection As RuleSetCollection
    End Class
End Namespace

